package com.frank.pipeline.controller;

import com.frank.pipeline.domain.PipelineVo;
import com.frank.pipeline.service.core.PipelineService;
import com.frank.pipeline.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author jianweilin
 * @date 2017/11/7
 */
@RestController
@Validated
public class PipelineController {
    @Autowired
    private PipelineService pipelineService;


    @RequestMapping(value = "/pipeline/list",method = RequestMethod.GET)
    public ApiResponse list(){
        return ApiResponse.success(pipelineService.getPipelineList());
    }

    @RequestMapping(value = "/pipeline/add",method = RequestMethod.POST)
    public ApiResponse add(@ModelAttribute @Valid PipelineVo pipelineVo){
        if(pipelineService.isExist(pipelineVo.getPipelineNo())){
            return ApiResponse.failed("管道编号已存在，不能重复添加！");
        }
        pipelineService.addPipelineInfo(pipelineVo);
        return ApiResponse.success();
    }

    @RequestMapping(value = "/pipeline/search",method = RequestMethod.GET)
    public ApiResponse<PipelineVo> search(@RequestParam Long pipelineNo){
        if(!pipelineService.isExist(pipelineNo)){
            return ApiResponse.failed("管道编号不存在,请重新输入");
        }
        PipelineVo pipelineVo = pipelineService.getPipelineInfo(pipelineNo);
        return pipelineVo == null ? ApiResponse.failed("管道编号不存在,请重新输入"): ApiResponse.success(pipelineVo);
    }

    @RequestMapping(value = "/pipeline/modify",method = RequestMethod.POST)
    public ApiResponse modify(@ModelAttribute @Valid PipelineVo pipelineVo){
        pipelineService.modify(pipelineVo);
        return ApiResponse.success("修改成功");
    }



}
